package cn.dankal.flyaudio.activity.detail

import android.content.Intent
import android.os.Bundle
import api.ActivityServiceFactory
import api.UserServiceFactory
import cn.dankal.basiclib.DKUserManager
import cn.dankal.basiclib.base.BaseStateView
import cn.dankal.basiclib.base.activity.BaseActivity
import cn.dankal.basiclib.base.callback.DKCallBackBoolean
import cn.dankal.basiclib.base.fragment.BaseRecyclerViewFragment
import cn.dankal.basiclib.protocol.MainProtocol
import cn.dankal.basiclib.rx.AbstractDialogSubscriber
import cn.dankal.basiclib.rx.AbstractStateSubscriber
import cn.dankal.basiclib.util.StringUtil
import cn.dankal.basiclib.util.ToastUtils
import cn.dankal.basiclib.widget.GenderDialog
import cn.dankal.business.pojo.JoinActivityBean
import cn.dankal.business.Constants
import cn.dankal.business.pojo.ActivityBean
import cn.dankal.business.pojo.BaseBeanList
import cn.dankal.business.pojo.LisenseBean
import cn.dankal.flyaudio.R
import cn.dankal.flyaudio.ResultActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_base_info.*
import kotlinx.android.synthetic.main.activity_entry_commit.*

/**
 * 活动报名
 */
@Route(path = MainProtocol.ENTRYCOMMIT)
class EntryCommitActivity : BaseActivity() {

    @Autowired(name = Constants.UUID)
    @JvmField
    var id: String? = null
    private lateinit var activityBean: ActivityBean;

    private lateinit var gender: String

    override fun getLayoutId(): Int {
        return R.layout.activity_entry_commit
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (intent?.extras != null && intent.extras.containsKey(Constants.UUID)) {
            id = intent?.extras?.getString(Constants.UUID)
        }
        if (intent?.extras != null && intent.extras.containsKey(ActivityBean::class.java.simpleName)) {
            activityBean = intent?.extras?.getSerializable(ActivityBean::class.java.simpleName) as ActivityBean
        }
    }

    override fun initComponents() {

        UserServiceFactory.vehicle_info_list("1", BaseRecyclerViewFragment.PAGE_SIZE)
                .subscribe(object : AbstractStateSubscriber<BaseBeanList<LisenseBean>>(null) {
                    override fun onNext(t: BaseBeanList<LisenseBean>) {
                        super.onNext(t)
                        if (t == null && t.list != null && t.list.size > 0) {
                            et_name.setText(t.list[0].name)
                            et_mobile.setText(t.list[0].phone)
                        }
                    }
                })

        bt_commit.setOnClickListener {
            if (validParams()) return@setOnClickListener
            commit()
        }
        tv_gender_choose_tow.setOnClickListener {
            GenderDialog(this, DKCallBackBoolean {
                when (it) {
                    DKCallBackBoolean.YES -> {
                        this@EntryCommitActivity.gender = "男"
                    }
                    else -> {
                        this@EntryCommitActivity.gender = "女"
                    }
                }
                et_gender.setText(gender)
            }).show()
        }
        tv_gender_choose.setOnClickListener {
            GenderDialog(this, DKCallBackBoolean {
                when (it) {
                    DKCallBackBoolean.YES -> {
                        this@EntryCommitActivity.gender = "男"
                    }
                    else -> {
                        this@EntryCommitActivity.gender = "女"
                    }
                }
                et_gender.setText(gender)
            }).show()
        }
        iv_close.setOnClickListener {
            finish()
        }
    }


    private fun validParams(): Boolean {
        if (activityBean?.applyRequired.isEmpty()) {
            return true
        }
        var name = et_name.text.toString()
        // 如果后台要求校验的话
        if (name.isNullOrEmpty() && activityBean?.applyRequired.contains(Constants.ApplyRequireType.name)) {
            ToastUtils.showShort(et_name.hint)
            return true
        }
        gender = et_gender.text.toString()
        if (gender.isNullOrEmpty() && activityBean?.applyRequired.contains(Constants.ApplyRequireType.sex)) {
            ToastUtils.showShort(et_gender.hint)
            return true
        }
        var age = et_age.text.toString()
        if (age.isNullOrEmpty() && activityBean?.applyRequired.contains(Constants.ApplyRequireType.age)) {
            ToastUtils.showShort(et_age.hint)
            return true
        }

        var mobile = et_mobile.text.toString()
        if (mobile.isNullOrEmpty() && activityBean?.applyRequired.contains(Constants.ApplyRequireType.phone)) {
            ToastUtils.showShort(et_mobile.hint)
            return true
        }
        // 如果手机号码为必填，需要教研手机的格式
        if (activityBean?.applyRequired.contains(Constants.ApplyRequireType.phone)) {
            if (!StringUtil.checkPhone(mobile)) {
                ToastUtils.showShort("请填写正确的手机格式")
                return true
            }
        }
        if (mobile.length < 11) {
            ToastUtils.showShort("请填写正确的手机格式")
            return true
        }

        var count = et_count.text.toString()
        if (count.isNullOrEmpty() && activityBean?.applyRequired.contains(Constants.ApplyRequireType.carryNumber)) {
            ToastUtils.showShort(et_count.hint)
            return true
        }
        var remark = et_remark.text.toString()
        if (remark.isNullOrEmpty() && activityBean?.applyRequired.contains(Constants.ApplyRequireType.remark)) {
            ToastUtils.showShort(et_remark.hint)
            return true
        }
        return false
    }

    private fun commit() {
        var joinActivityBean = JoinActivityBean()
        joinActivityBean.name = et_name.text.trim().toString()
        joinActivityBean.age = et_age.text.trim().toString()
        joinActivityBean.carryNumber = et_count.text.trim().toString()
        joinActivityBean.remark = et_remark.text.trim().toString()
        joinActivityBean.sex = gender
        joinActivityBean.phone = et_mobile.text.trim().toString()
        joinActivityBean.userUuid = DKUserManager.getUserInfo().userUuid
        joinActivityBean.vehicleActivityUuid = id
        ActivityServiceFactory.user_apply_activity(joinActivityBean)
                .subscribe(object : AbstractDialogSubscriber<String>(this) {
                    override fun onNext(t: String) {
                        startActivity(Intent(this@EntryCommitActivity, ResultActivity::class.java))
                        finish()
                    }
                })
    }
}
